package com.example.weiwing.home.study;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.TypeReference;
import com.example.weiwing.R;
import com.example.weiwing.config.WeiWingConfig;
import com.example.weiwing.entity.Article;
import com.example.weiwing.entity.Course;
import com.example.weiwing.entity.Lesson;
import com.example.weiwing.home.dynamic.DynamicFragment;
import com.example.weiwing.util.SpUtils;
import com.kongzue.baseokhttp.HttpRequest;
import com.kongzue.baseokhttp.listener.ResponseListener;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class StudyFragment extends Fragment {

    private RecyclerView rvLesson;

    private Handler mHandler;
    private List<Course> courses = new ArrayList<>();
    private CourseShowAdapter mCourseShowAdapter;

    private int pageNum = 1;
    private int pageSize = 5;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_study, container, false);

        // 设置视频展示的 RecyclerView
        rvLesson = view.findViewById(R.id.studyLessonRecyclerView);
        LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(getActivity());
        mLinearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvLesson.setLayoutManager(mLinearLayoutManager);

        mHandler = new Handler(Looper.getMainLooper()) {
            @Override
            public void handleMessage(@NonNull Message msg) {
                super.handleMessage(msg);
                if (msg.what == 1) {

                }
            }
        };

        new CourseListThread(pageNum, pageSize, 3).start();

        // TODO： 此处应该是从服务器获取课程数据
        // 以下是模拟的 Course 数据
//        courses = new ArrayList<>();
//        for(int i = 1 ; i <= 10 ; i ++ ) {
//            Course course = new Course();
//            course.setTitle("这是第" + i + "门课程");
//            course.setVideoView(100000L);
//            course.setTotolTime(998244353L);
//            course.setImgUrl("https://i1.hdslb.com/bfs/archive/c6e4ea1ec234f702ae96a3de64ba394dd4f9f0cf.jpg@672w_378h_1c_!web-search-common-cover.webp");
//            course.setAuthor("Weiqt");
//            course.setPublishTime(new Date());
//            course.setLessonCount(1L);
//            List<Lesson> lessons = new ArrayList<>();
//            Lesson lesson = new Lesson();
//            lesson.setLessonName("第一集");
//            lesson.setVideoUrl("http://10.41.109.210:80/profile/course_video/2023/12/12/528704eba89ea45dc2e895624b1b6b4f/ts/index.m3u8");
//            lesson.setEposode(0);
//            lessons.add(lesson);
//            Lesson lesson2 = new Lesson();
//            lesson2.setLessonName("第二集");
//            lesson2.setVideoUrl("http://118.178.132.143/hls/video1/test1.m3u8");
//            lesson2.setEposode(1);
//            lessons.add(lesson2);
//            course.setLessonList(lessons);
//            courses.add(course);
//        }

        mCourseShowAdapter = new CourseShowAdapter(getActivity(),courses);
        rvLesson.setAdapter(mCourseShowAdapter);

        return view;
    }

    public class CourseListThread extends Thread {
        private int pageNum;
        private int pageSize;
        private int i;

        public CourseListThread(int pageNum, int pageSize, int i) {
            super();
            this.pageNum = pageNum;
            this.pageSize = pageSize;
            this.i = i;
        }

        @Override
        public void run() {
            // 请求加载更多数据
            HttpRequest.build(getActivity(), WeiWingConfig.getUrl() + "/course/list")
                    .addHeaders("token", SpUtils.getString(getActivity(), "token"))
                    .addParameter("pageNum", pageNum)
                    .addParameter("pageSize", pageSize)
                    .setResponseListener(new ResponseListener() {
                        @Override
                        public void onResponse(String response, Exception error) {
                            // 处理加载结果
                            if (error == null) {
                                JSONObject jsonObject = JSON.parseObject(response);
                                if (jsonObject.getInteger("code") == 200) {
                                    JSONArray rows = jsonObject.getJSONArray("rows");
                                    List<Course> courseList = JSON.parseObject(rows.toJSONString(), new TypeReference<List<Course>>() {
                                    });
                                    // 首先发送消息
                                    mHandler.obtainMessage(i).sendToTarget();
                                    // 然后在主线程中更新 UI
                                    courses.addAll(courseList);
                                    mCourseShowAdapter.notifyDataSetChanged();
                                } else {
                                    Toast.makeText(getActivity(), "请求失败", Toast.LENGTH_SHORT).show();
                                }
                            } else {
                                Toast.makeText(getActivity(), "请求失败", Toast.LENGTH_SHORT).show();
                            }
                        }
                    }).doGet();
        }
    }
}


